<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Laravue\Models\CourseModel;
use \App\Laravue\JsonResponse;
use \App\Laravue\Faker;
class CourseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(CourseModel $courseModel,Request $request)
    {
        $data = [
            'data'=>$courseModel::offset(($request->query('page') - 1 ) * $request->query('limit'))->limit($request->query('limit'))->get()->toArray(),
            'total'=>$courseModel::count()
        ];
        return response()->json(new JsonResponse(['items' => $data]));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(CourseModel $courseModel,Request $request)
    {
        //
        return "创建";
//        return response()->json();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CourseModel $courseModel,Request $request)
    {
        $course = $courseModel::create($request->all());
        if ($course->save()){
            return "添加成功";
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(CourseModel $courseModel,$keyword)
    {

        $course = $courseModel::where('course_name','like','%'.$keyword.'%')->get();
        return response()->json(new JsonResponse(['items' => $course]));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CourseModel $courseModel,Request $request, $id)
    {
        //
//        return $request->all();
        $course = $courseModel::find($id);
        $course->update($request->all());
        return '修改成功';
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(CourseModel $courseModel,$id)
    {
        //
//        return $id;
        $course = $courseModel::find($id);
        if ($course->delete()){
            return "删除成功";
        }
    }
}
